package com.formula.datastructure.exam.other;

import com.formula.datastructure.util.DataUtil;

public class SortAlmost {
    public Integer[] sort(Integer[] array) {
        int N = array.length;

        reverse(array);
        int flag = 1;
        Integer temp;
        for (int i = 0; i < N; i++) {
            if (flag == 0) {
                break;
            } else {
                flag = 0;
            }
            for (int j = 0; j < N - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    flag = 1;
                    temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }

        return array;
    }

    private void reverse(Integer[] array) {
        int min = 0;
        int max = array.length - 1;
        Integer temp;
        while (min < max) {
            temp = array[min];
            array[min] = array[max];
            array[max] = temp;
            min++;
            max--;
        }
    }

    public static void main(String[] args) {
        MaxK cal = new MaxK();
        int size = 20;
        int range = 99;
        Integer[] array = DataUtil.genUniqueArray(size, range);
        Integer[] input = cal.maxK(array, 20);
        input[15] = 17;
        input[3] = 50;
        input[9] = 33;
        DataUtil.printIndex(size);
        DataUtil.printArray(input);

        SortAlmost a = new SortAlmost();
        DataUtil.printArray(a.sort(input));
    }
}
